import java.util.*;


public class Solution4 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param num int整型一维数组 
     * @return int整型ArrayList<ArrayList<>>
     */
    public ArrayList<ArrayList<Integer>> threeSum (int[] num) {
        // write code here
        Arrays.sort(num);
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        for(int i = 0;i<num.length;i++) {
            if(num[i] > 0) {
                continue;
            }
            int target = -num[i];
            int left = i+1;
            int right = num.length - 1;
            while(left < right) {
                if(num[left] + num[right] == target) {
                    ArrayList<Integer> list = new ArrayList<>();
                    list.add(-target);
                    list.add(num[left++]);
                    list.add(num[right--]);
                    if(!res.contains(list)) {
                        res.add(list);  
                    }
                    
                } else if(num[left] + num[right] > target) {
                    right--;
                } else {
                    left++;
                }
            }
        }
        return res;
    }
}